package com.jrtc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Disease;
import com.jrtc.base.entity.bo.Diseasecategory;
import com.jrtc.dao.DiseasecategoryDao;
import com.jrtc.base.entity.vo.DiseaseVo;
import com.jrtc.dao.DiseaseDao;
import com.jrtc.service.DiseaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 疾病表(Disease)表服务实现类
 *
 * @author makejava
 * @since 2022-03-21 17:34:11
 */
@Service("diseaseService")
public class DiseaseServiceImpl implements DiseaseService
{
    @Resource
    private DiseaseDao diseaseDao;
    @Resource
    private DiseasecategoryDao diseasecategoryDao;

    /**
     * 通过ID查询单条数据
     *
     * @param diseaseId 主键
     * @return 实例对象
     */
    @Override
    public Disease queryById(Integer diseaseId) {
        return this.diseaseDao.queryById(diseaseId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Disease> queryAllByLimit(int offset, int limit) {
        return this.diseaseDao.queryAllByLimit(offset, limit);
    }

    @Override
    public IPage<Disease> queryDiseaseList(Disease disease)
    {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Disease> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(disease.getPageNo(), disease.getPageSize());
        IPage<Disease> diseasePage = this.diseaseDao.queryDiseaseList(page, disease);
        return diseasePage;
    }
    /**
     * 新增数据
     *
     * @param disease 实例对象
     * @return 实例对象
     */
    @Override
    public Disease insert(Disease disease) {
        this.diseaseDao.insert(disease);
        return disease;
    }

    /**
     * 修改数据
     *
     * @param disease 实例对象
     * @return 实例对象
     */
    @Override
    public Disease update(Disease disease) {
        this.diseaseDao.update(disease);
        return this.queryById(disease.getDiseaseId());
    }

    /**
     * 通过主键删除数据
     *
     * @param diseaseId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer diseaseId) {
        return this.diseaseDao.deleteById(diseaseId) > 0;
    }

    /**
     * 查询疾病信息
     * */
    @Override
    public List<DiseaseVo> selectDisease(Integer diseasegategoryId) throws Exception {
        List<Disease> diseases;
        if (diseasegategoryId != null && diseasegategoryId != 0){
            diseases = diseaseDao.selectDiseaseByDiseasegategoryId(diseasegategoryId);
        }
        else {
            diseases = diseaseDao.selectDisease();
        }
        if (diseases != null && diseases.size() > 0){
            List<DiseaseVo> diseaseVos = new ArrayList<>();
            for (Disease disease : diseases) {
                DiseaseVo diseaseVo = new DiseaseVo();
                diseaseVo.setDiseaseId(disease.getDiseaseId());
                diseaseVo.setDiseasegategoryId(disease.getDiseasegategoryId());
                diseaseVo.setDiseaseName(disease.getDiseaseName());

                if (disease.getDiseasegategoryId() != null && disease.getDiseasegategoryId() != 0){
                    diseaseVo.setDiseasegategoryId(disease.getDiseasegategoryId());
                    Diseasecategory diseasecategory = diseasecategoryDao.queryById(disease.getDiseasegategoryId());
                    if (diseasecategory != null){
                        diseaseVo.setDiseasegategoryName(diseasecategory.getDiseasecategoryName());
                    }
                }

                diseaseVos.add(diseaseVo);
            }
            return diseaseVos;
        }
        else {
            throw new Exception("却查到疾病信息");
        }
    }
}